TKey
TValue
CSharpTest.Net
KeyValueUpdate<TKey,TValue> Delegate
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Collections Namespace : KeyValueUpdate<TKey,TValue> Delegate

key
original

Glossary Item Box

Provides a delegate that performs an atomic update of a key/value pair

Syntax

Visual Basic (Declaration) 
Public Delegate Function KeyValueUpdate
    (Of TKey,TValue)( _
   ByVal key As TKey, _
   ByVal original As TValue _
) As TValue
C# 
public delegate TValue KeyValueUpdate<TKey,TValue>( 
   TKey key,
   TValue original
)

Parameters

key
original

Type Parameters

TKey
TValue

Example

Library/Library.Test/TestSynchronizedCollections.cs

C#Copy Code
SynchronizedDictionary<int, string> data =
    new SynchronizedDictionary<int, string>(new Dictionary<int, string>());

Assert.IsTrue(data.TryAdd(1, "a"));
Assert.IsFalse(data.TryAdd(1, "a"));

Assert.IsTrue(data.TryUpdate(1, "a"));
Assert.IsTrue(data.TryUpdate(1, "c"));
Assert.IsTrue(data.TryUpdate(1, "d", "c"));
Assert.IsFalse(data.TryUpdate(1, "f", "c"));
Assert.AreEqual("d", data[1]);
Assert.IsTrue(data.TryUpdate(1, "a", data[1]));
Assert.AreEqual("a", data[1]);
Assert.IsFalse(data.TryUpdate(2, "b"));

string val;
Assert.IsTrue(data.TryRemove(1, out val) && val == "a");
Assert.IsFalse(data.TryRemove(2, out val));
Assert.AreNotEqual(val, "a");

Assert.IsFalse(data.TryUpdate(1, (k, x) => x.ToUpper()));
data[1] = "a";
data[1] = "b";
Assert.IsTrue(data.TryUpdate(1, (k, x) => x.ToUpper()));
Assert.AreEqual("B", data[1]);
VB.NETCopy Code
Dim data As New SynchronizedDictionary(Of Integer, String)(New Dictionary(Of Integer, String)())

Assert.IsTrue(data.TryAdd(1, "a"))
Assert.IsFalse(data.TryAdd(1, "a"))

Assert.IsTrue(data.TryUpdate(1, "a"))
Assert.IsTrue(data.TryUpdate(1, "c"))
Assert.IsTrue(data.TryUpdate(1, "d", "c"))
Assert.IsFalse(data.TryUpdate(1, "f", "c"))
Assert.AreEqual("d", data(1))
Assert.IsTrue(data.TryUpdate(1, "a", data(1)))
Assert.AreEqual("a", data(1))
Assert.IsFalse(data.TryUpdate(2, "b"))

Dim val As String
Assert.IsTrue(data.TryRemove(1, val) AndAlso val = "a")
Assert.IsFalse(data.TryRemove(2, val))
Assert.AreNotEqual(val, "a")

Assert.IsFalse(data.TryUpdate(1, Function(k, x) x.ToUpper()))
data(1) = "a"
data(1) = "b"
Assert.IsTrue(data.TryUpdate(1, Function(k, x) x.ToUpper()))
Assert.AreEqual("B", data(1))

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys